$(function(){
	menuZtreeInfo();
	//初始化form表单
	var form = $("#form_menu").Validform({
		tiptype:2, //可选项 1=>pop box,2=>side tip(parent.next.find; with default pop),3=>side tip(siblings; with default pop),4=>side tip(siblings; none pop)，默认为1，也可以传入一个function函数，自定义提示信息的显示方式（可以实现你想要的任何效果，具体参见demo页）;
		showAllError:false,//可选项 true | false，true：提交表单时所有错误提示信息都会显示，false：一碰到验证不通过的就停止检测后面的元素，只显示该元素的错误信息;
		postonce:true, //可选项 表单是否只能提交一次，true开启，不填则默认关闭;
		ajaxPost:true, //使用ajax方式提交表单数据，默认false，提交地址就是action指定地址;
		callback:function(data){
			if(data.status=="T"){
				menuZtreeInfo();
				$("#myModal").modal("hide");
			}else{
				layer.msg(data.info);
				form.resetStatus();
			}
		}
	});
	//初始化需要校验的内容
	form.addRule([
  		{
  			ele:"#menu-cname",
  			datatype:"*1-8",
  			errormsg:"请输入1-8个长度的字符"
  		},
  		{
  			ele:"#menu-ename",
  			datatype:"*1-15",
  			errormsg:"请输入1-15个长度的字符"
  		},
  		{
  			ele:"#menu-sequence",
  			datatype:"*",
  			errormsg:"请选择序列"
  		}
  	]);
	function menuZtreeInfo(){
		var zNodes =new Array();
		$.ajax({
			url:htx+"/menu/getMenuToJSON",
			async: false,
			type:"POST",
			dataType:"JSON",
			success:function(data){
				if(data.status=="T"){
					var info = data.info;
					for(var i=0;i<info.length;i++) {
						var json = {id:info[i].id,pId:info[i].parentMenuId,name:info[i].cname};
						zNodes.push(json);
					}
				}
			}
		});
		//初始化菜单树参数
		var setting = {
			view: {
				dblClickExpand: true,
				showLine: true,
				selectedMulti: false,
				showIcon: true,
				icon:"Hui-iconfont-slider-right"
			},
			callback: {
				onClick: function(event, treeId, treeNode){
					var id = treeNode.id;
					$.ajax({
						url:htx+"/res/getResourceByMenuId",
						data:{menuId:id},
						type:"POST",
						dataType:"JSON",
						success:function(data){
							var table = $("#resource_info");
							var tbody=table.find("tbody");
							if(data.status=="T"){
								var res = data.info;
								tbody.empty();
								table.find("tr").eq(1).clone(true).appendTo(tbody);
								var tr =tbody.find("tr:last");
								var td = tr.find("td");
								$(td[0]).html(1);
								$(td[1]).html(res.action);
								$(td[2]).html(res.method);
								$(td[3]).html(res.url);
								$(td[4]).html(res.chineseStatus);
								tr.removeAttr("style");
							}else{
								tbody.empty();
							}
						}
					});
				}
			},
			data: {
				simpleData: {
				enable:true,
				idKey: "id",
				pIdKey: "pId",
				rootPId: ""
				}
			}
		};
		admin.show_ztree(zNodes,"menu-tree",true,false,setting);
	}
	//初始化菜单序列下拉框
	for(var i=1;i<=25;i++){
		$('<option />', {value:i, text:i}).appendTo($("#menu_div select"));
	}
	$("#addMenu_btn").click(function(){
		form.resetForm();//清除表单参数
		var zTree = $.fn.zTree.getZTreeObj("menu-tree");
		var checkMenu=zTree.getSelectedNodes();
		if(checkMenu==null || checkMenu=="" || checkMenu==undefined){
			//未选中任何菜单，表示为一级菜单
			$("#level_num").val("1");
			$("#menu-del").hide();
			admin.dialog_show("添加菜单","#myModal","#menu_div",500,400);
		}else if(checkMenu[0].level>=2){
			layer.msg("已达到最大菜单级数，无法增加下一级菜单！");
		}else{
			$("#parent_menu_id").val(checkMenu[0].id);
			$("#level_num").val(checkMenu[0].level+1);
			admin.dialog_show("添加菜单","#myModal","#menu_div",500,400);
			$("#menu-del").hide();
		}
	});
	$("#updateMenu_btn").click(function(){
		form.resetForm(); //清除原表单状态
		var zTree = $.fn.zTree.getZTreeObj("menu-tree");
		var checkMenu=zTree.getSelectedNodes();
		if(checkMenu==null || checkMenu=="" || checkMenu==undefined){
            layer.msg("请选择要修改的菜单");
			return false;
		}
		var nodeId= checkMenu[0].id;
		$.ajax({
			url:htx+"/menu/getMenuById",
			data:{id:nodeId},
			type:"POST",
			dataType:"JSON",
			success:function(data){
				if(data.status=="T"){
					var info = data.info;
					$("#menu_div #menu-cname").val(info.cname);
					$("#menu_div #menu-ename").val(info.ename);
					$("#menu_div #menu-id").val(data.info.id);
					$("#menu_div #menu-sequence").val(info.sequence);
					admin.dialog_show("修改菜单","#myModal","#menu_div",500,400);
				}else{
					layer.msg(data.info);
				}
			}
		});
	});

	$("#search_resource").click(function(){
		var key = $("#search_key").val();
		if(key==null || key=="" || key==undefined){
            layer.msg("请输入查询关键字");
			return false;
		}
		$.ajax({
			url:htx+"/res/getResourceByLikeKey",
			data:{searchKey:key},
			type:"POST",
			dataType:"JSON",
			success:function(data){
				if(data.status=="T"){
					var info = data.info;
					var table = $("#resource_talbe");
					var tbody=table.find("tbody");
					tbody.empty();
					for(var i=0;i<info.length;i++) {
						var res = info[i];
						table.find("tr").eq(1).clone(true).appendTo(tbody);
						var tr =tbody.find("tr:last");
						var td = tr.find("td");
						$(td[0]).html(i+1);
						$(td[1]).html(res.action);
						$(td[2]).html(res.method);
						$(td[3]).html(res.url);
						$(td[4]).html(res.chineseStatus);
						$(td[5]).find("a[name=bindResource_btn]").data("id",res.id);
						tr.removeAttr("style");
					}
				}else{
					layer.msg(data.info);
				}
			}
		});
	});
	$("a[name=bindResource_btn]").click(function(){
		var zTree = $.fn.zTree.getZTreeObj("menu-tree");
		var checkMenu=zTree.getSelectedNodes();
		if(checkMenu==undefined || checkMenu==null || checkMenu==""){
            layer.msg("请选择要绑定资源的菜单");
			return false;
		}
		var nodeId= checkMenu[0].id;
		var nodeName = checkMenu[0].name;
		var resourceId= $(this).data("id");
		layer.confirm(
			"是否将此资源连接绑定到菜单【"+nodeName+"】上？<br />注：如果已绑定过权限资源则将会替换旧资源",
			{
				btn:["确定","取消"],
				title:"提示"
			},
			function(){
				$.ajax({
					url:htx+"/menu/bindResource",
					data:{menuId:nodeId,resId:resourceId},
					type:"POST",
					dataType:"JSON",
					success:function(data){
						if(data.status=="T"){
							layer.msg("绑定成功",{time:800},function(){window.location.href=window.location.href;});
						}else{
							layer.msg(data.info);
						}
					}
				});
			}
		);
	});

	$("a[name=unBind_btn]").click(function(){
		var zTree = $.fn.zTree.getZTreeObj("menu-tree");
		var checkMenu=zTree.getSelectedNodes();
		if(checkMenu==undefined || checkMenu==null || checkMenu==""){
			layer.msg("请选择要解除绑定的菜单");
			return false;
		}
		layer.confirm(
			"是否解除菜单【"+checkMenu[0].name+"】上绑定的资源",
			{
				btn:["解除","取消"]
			},
			function(){
				$.ajax({
					url:htx+"/menu/unBindResource",
					data:{menuId:checkMenu[0].id},
					type:"POST",
					dataType:"JSON",
					success:function(data){
						if(data.status=="T"){
							layer.msg("解除成功",{time:800},function(){window.location.href=window.location.href;});
						}else{
							layer.msg(data.info);
						}
					}
				});
			}
		);
	});
	$("#deleteMenu_btn").click(function(){
		var id;
        var checkMenu =  admin.getFocusZtree("menu-tree");
		if(checkMenu==null || checkMenu=="" || checkMenu==undefined){
			layer.msg("请选择要删除的菜单");
			return false;
		}else{
            id=checkMenu[0].id
        }
		layer.confirm(
			'是否删除',
			{
				icon: 3
			},
			function(){
				$.ajax({
					url:htx+"/menu/delMenu",
					data:{id:id},
					type:"POST",
					dataType:"JSON",
					success:function(data){
						if(data.status=="T"){
							layer.msg("删除成功",{time:800},function(){window.location.href=window.location.href;});
						}else{
							layer.msg(data.info);
						}
					}
				});
			}
		);
	});
});